#! node
// var param=process.argv;
// console.log(param)
// console.log('hello world')

var fs = require ('fs');
var path = require ('path');
var util = require ('util');
var xml2js=require('xml2js');
var parseString=xml2js.parseString;

var param = process.argv.slice(2);
var config = {};

param.forEach ( (item) => {
  switch (item) {
    case "wuxijin":
      config[item]=item;
      break;
    case "wuchunju":
      config[item]=item;
      break;
    default:
      break;
  }
} )


//ceshi
var xml=''
const readFileAsync=util.promisify(fs.readFile);
const writeFileAsync=util.promisify(fs.writeFile);
const existsFileAsync=util.promisify(fs.exists);
const unlinkFileAsync=util.promisify(fs.unlink);

async function toXML () {
  xml = await readFileAsync(path.resolve(__dirname,'./template/test.xml'), 'utf-8');
  console.log(xml);
  parseString(xml, function (err, result) {
      console.dir(JSON.stringify(result));
  });
  let test1=await existsFileAsync(path.resolve(__dirname,'./template/test1.xml'));
  if(test1){
    try{
      await unlinkFileAsync(path.resolve(__dirname,'./template/test1.xml'));
    }catch(e){
      console.log(e)
    }
    // await writeFileAsync(path.resolve(__dirname,'./template/test1.xml'),xml);
    // console.log('writeEnd')
  }else {
    await writeFileAsync(path.resolve(__dirname,'./template/test1.xml'),xml);
    console.log('writeEnd')
  }
  // await writeFileAsync(path.resolve(__dirname,'./template/test1.xml'),xml);
  // console.log('writeEnd')
}


toXML();

// xml=fs.readFileSync(path.resolve(__dirname,'./template/test.xml'), 'utf-8')
// console.log(xml);
// parseString(xml, function (err, result) {
//     console.dir(JSON.stringify(result));
// });
